package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.acticle.domain.CityScenic;
import cn.wolfcode.luowowo.acticle.domain.CityScenicDetail;
import cn.wolfcode.luowowo.acticle.domain.Destination;
import cn.wolfcode.luowowo.acticle.query.CityScenicQueryObject;
import cn.wolfcode.luowowo.acticle.service.ICityScenicDetailService;
import cn.wolfcode.luowowo.acticle.service.ICityScenicService;
import cn.wolfcode.luowowo.acticle.service.IDestinationService;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.query.ScenicCommentQueryObject;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cityScenic")
public class CityScenicController {

    @Reference
    private IDestinationService destinationService;

    @Reference
    private ICityScenicService cityScenicService;

    @Reference
    private ICityScenicDetailService cityScenicDetailService;

    @Reference
    private IScenicCommentService scenicCommentService;

    @RequestMapping("/list")
 public String list(long id, @ModelAttribute("qo") CityScenicQueryObject qo, Model model){
        //获取地区id,共享吐司
        //toasts
        List<Destination> toasts = destinationService.getToasts(id);
        Destination destination = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("totasBoss",destination.getName() +"景点");
        //dest
        model.addAttribute("dest", destination);
        //pageInfo
        model.addAttribute("pageInfo",cityScenicService.query(qo));


        //all
        model.addAttribute("all",cityScenicService.queryAll());

        //hots 共享热门信息
        List<CityScenic> hots = cityScenicService.queryByHot();
        model.addAttribute("hots",hots);

        return "cityscenic/test";
    }

    @RequestMapping("/detail")
    public String detail(CityScenicDetail detail,Model model,@ModelAttribute("qo") ScenicCommentQueryObject qo){

        CityScenicDetail csd = cityScenicDetailService.queryForOne(detail);
        csd.setLastUpdateTime(new Date());
        model.addAttribute("detail",csd);

        Page<ScenicComment> page = scenicCommentService.query(qo,detail);
        model.addAttribute("page", page);

        return "cityscenic/scenic_detail";
    }


    @RequestMapping("/commentAdd")
    @RequireLogin
    @ResponseBody
    public Object commentAdd(ScenicComment scenicComment, @UserParam UserInfo userInfo){
        scenicComment.setUserId(userInfo.getId());
        scenicComment.setUsername(userInfo.getNickname());
        scenicComment.setCity(userInfo.getCity());
        scenicComment.setLevel(userInfo.getLevel());
        scenicComment.setHeadUrl(userInfo.getHeadImgUrl());



        scenicCommentService.saveOrUpdate(scenicComment);

        return new AjaxResult();
    }

   /* @RequestMapping("/comment")
    public String comment(@ModelAttribute ScenicCommentQueryObject qo, Model model){
        //page  //page.content
        //spring-data提供的分类封装对象
        Page<ScenicComment> page = scenicCommentService.query(qo);
        model.addAttribute("page", page);
        return "cityscenic/commentTpl";
    }*/

    //点赞
    @ResponseBody
    @RequireLogin
    @RequestMapping("/commentThumbUp")
    public Object commentThumbUp(String cid, @UserParam UserInfo userInfo){
        boolean ret = scenicCommentService.thumbUp(cid, userInfo.getId());
        return new AjaxResult(ret, "");
    }





    @Value("${file.path}")
    private String filePath;

    @RequestMapping("/images")
    @ResponseBody
    public Map upload(MultipartFile file) {
        //为了配合后期的开发用插件返回,这里要用Map存储数据
        HashMap<String, Object> map = new HashMap<>();
        try {

            String url =UploadUtil.upload(file, filePath);
            map.put("status", 1);//上传成功传个1
            map.put("url", url);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status", 0);//上传失败 传个0
            map.put("msg", "上传失败");


        }

        return map;
    }
}
